package com.test.first;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class RateListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    RateAdapter adapter;
    private static final String TAG = "RateListActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list);
        //button
        listView = findViewById(R.id.mylist);
        listView.setOnItemClickListener(this);

        Handler handler = new Handler(){
            @Override
            public void handleMessage(@Nullable Message msg){
                if(msg.what==11){
                    //ArrayList<HashMap<String,String>>retList = (ArrayList<HashMap<String, String>>)msg.obj;
                   ArrayList<Rate> ret = (ArrayList<Rate>)msg.obj;
                   adapter = new RateAdapter(RateListActivity.this,R.layout.list_item,ret);

                   listView.setAdapter(adapter);

                }
                super.handleMessage(msg);
            }
        };

        RateTask task = new RateTask();
        task.setHandler(handler);

        Thread t = new Thread(task);
        t.start();//task.run()

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object itemAtPosition = listView.getItemAtPosition(position);
        Rate item = (Rate)itemAtPosition;
        String titleStr = item.getCname();
        String detailStr = item.getCval();
        Log.i(TAG,"onItemClick: titleStr="+ titleStr);
        Log.i(TAG,"OnItemClick: detailStr=" + detailStr);

        //删除数据

        adapter.remove(item);


        //打开窗口
//        Intent calc = new Intent(this,CalcActivity.class);
//        calc.putExtra("title",titleStr);
//        calc.putExtra("rate",detailStr);
//        startActivity(calc);

    }
}